digit = int(input())
max_prost = 0
if len(str(digit)) == 4:
    for i in range(2, digit + 1):
        if str(i) in str(digit):
            w = False
            for v in range(2, i // 2 + 1):
                if i % v == 0:
                    w = True
                    break
            if w:
                continue
            else:
                max_prost = max(max_prost, i)
print(max_prost)

